<?php

namespace services\base;

use common\components\Service;
use common\models\buss\CommonRelation;
use Yii;

/**
 * Class CommonRelationService
 * @package services\base
 */
class CommonRelationService extends Service
{
    /**
     * @param int|array $id
     * @param int $type
     * @return CommonRelation[]
     */
    public static function getCommonRelationListByPrimaryId($id, int $type): array
    {
        return CommonRelation::find()
            ->where([
                'primary_id' => $id,
                'type' => $type,
            ])
            ->all();
    }

    /**
     * @param int $type
     * @return CommonRelation[]
     */
    public static function getCommonRelationListByType(int $type): array
    {
        return CommonRelation::find()
            ->where([
                'type' => $type,
            ])
            ->all();
    }

    /**
     * @param int|array $id
     * @param int $type
     * @return CommonRelation[]
     */
    public static function getCommonRelationListByRelationId($id, int $type): array
    {
        return CommonRelation::find()
            ->where([
                'relation_id' => $id,
                'type' => $type,
            ])
            ->all();
    }

    /**
     * 根据工具ID获取标签IDs
     * @param int|array $toolId
     * @return int[]
     */
    public static function getToolTagIds($toolId): array
    {
        $data = self::getCommonRelationListByPrimaryId($toolId, CommonRelation::TYPE_TOOL_TAG);
        return $data ? array_column($data, 'relation_id') : [];
    }

    /**
     * 根据工具ID获取标签IDs
     * @param int|array $toolId
     * @return int[]
     */
    public static function getToolCategoryIds($toolId): array
    {
        $data = self::getCommonRelationListByPrimaryId($toolId, CommonRelation::TYPE_TOOL_CATEGORY);
        return $data ? array_column($data, 'relation_id') : [];
    }

    /**
     * 根据标签IDs获取工具ID
     * @param int|array $tagIds
     * @return int[]
     */
    public static function getTagToolIds($tagIds): array
    {
        $data = self::getCommonRelationListByRelationId($tagIds, CommonRelation::TYPE_TOOL_TAG);
        return $data ? array_column($data, 'primary_id') : [];
    }

    /**
     * 根据内容ID获取标签IDs
     * @param int|array $contentId
     * @return int[]
     */
    public static function getContentTagIds($contentId): array
    {
        $data = self::getCommonRelationListByPrimaryId($contentId, CommonRelation::TYPE_CONTENT);
        return $data ? array_column($data, 'relation_id') : [];
    }

    /**
     * 根据标签IDs获取内容ID
     * @param int|array $tagIds
     * @return int[]
     */
    public static function getTagContentIds($tagIds): array
    {
        $data = self::getCommonRelationListByRelationId($tagIds, CommonRelation::TYPE_CONTENT);
        return $data ? array_column($data, 'primary_id') : [];
    }

    /**
     * 根据分类IDs获取工具ID【获取满足所有分类ID条件工具IDs】
     * @param int|array $categoryIds
     * @return int[]
     */
    public static function getEqualCategoryToolIds($categoryIds): array
    {
        $data = CommonRelation::find()
            ->select(['primary_id', 'COUNT(*) cnt'])
            ->where([
                'relation_id' => $categoryIds,
                'type' => CommonRelation::TYPE_TOOL_CATEGORY,
            ])
            ->having(['=', 'cnt', count($categoryIds)])
            ->groupBy('primary_id')
            ->asArray()
            ->all();
//        $data = self::getCommonRelationListByRelationId($tagIds, CommonRelation::TYPE_TOOL);
        return $data ? array_column($data, 'primary_id') : [];
    }

    /**
     * 更新绑定关系
     * @param int $type
     * @param int $primaryId
     * @param $addIds
     * @param $delIds
     * @return void
     * @throws \yii\db\Exception
     */
    public static function editModel(int $type, int $primaryId, array $addIds, array $delIds = []): void
    {
        if ($delIds) {
            CommonRelation::deleteAll([
                'primary_id' => $primaryId,
                'relation_id' => $delIds,
                'type' => $type,
            ]);
        }
        if ($addIds) {
            $data = [];
            foreach ($addIds as $id) {
                $data[] = [
                    $primaryId, $id, $type,
                    Yii::$app->user->identity->member_id,
                    Yii::$app->user->identity->member->realname,
                    time(),
                    time()
                ];
            }
            Yii::$app->db->createCommand()->batchInsert(
                CommonRelation::tableName(),
                ['primary_id', 'relation_id', 'type', 'operation_id', 'operation_name', 'created_at', 'updated_at'],
                $data
            )->execute();
        }
    }

    /**
     * 解绑操作【关联ID】
     * @param int $type
     * @param int $relationId
     * @return void
     */
    public static function unbindByRelationId(int $type, int $relationId): void
    {
        CommonRelation::deleteAll([
            'relation_id' => $relationId,
            'type' => $type,
        ]);
    }

    /**
     * 解绑【主键ID】
     * @param int $type
     * @param int $primaryId
     * @return void
     */
    public static function unbindByPrimaryId(int $type, int $primaryId): void
    {
        CommonRelation::deleteAll([
            'primary_id' => $primaryId,
            'type' => $type,
        ]);
    }

}
